package com.xhga.common;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;

import java.util.List;

/**
 * Created by Hua wb on 2018/11/29.
 */
public class RpcDecoder extends ByteToMessageDecoder {

    private Class<?> cls;

    public RpcDecoder(Class<?> cls) {
        this.cls = cls;
    }

    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
        int size = in.readableBytes();
        if (size < 4) {
            return;
        }
        byte[] bytes = new byte[size];
        in.readBytes(bytes);
        Object object = ProtostuffUtil.deSerialize(bytes, cls);
        out.add(object);
        ctx.flush();
    }
}
